<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use App\Constants\Security;
use App\Model\WebUser;
use App\Services\loginServices;
use Hyperf\Utils\Context;
use Hyperf\View\RenderInterface;
use Hyperf\HttpServer\Contract\RequestInterface;

class IndexController extends AbstractController
{
    public function index(RenderInterface $render)
    {
        $user = Context::get("user");
        return $render->render('talk', $user);
    }

    public function loginView (RenderInterface $render)
    {
        return $render->render('login', ["msg" => ""]);
    }

    public function login(RequestInterface $request)
    {
        $name = $request->input('auth', '');
        $pass = $request->input('pass', '');

        if (empty($name) || empty($pass)) {
            return $this->toErrorJson([], "账号或密码为空");
        }

        $result = loginServices::login($name, $pass);

        if ($result["status"]) {
            return $this->toSuccessJson($result, "登录成功");
        } else {
            return $this->toErrorJson([], $result["msg"]);
        }

    }

    public function encrypt()
    {
        $key = "root:123456789as";
        $pass = Security::encrypt("123456", $key);
        print_r($pass);
        //pvHHE4r31k0yC82me2bi0A==
        $pass2 = Security::decrypt($pass, $key);
        print_r($pass2);
        //123456
        return "1";
    }

}
